package cc.mrbird.febs.pedometer.service.impl;

import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.pedometer.entity.Batches;
import cc.mrbird.febs.pedometer.entity.Chicken;
import cc.mrbird.febs.pedometer.mapper.ChickenMapper;
import cc.mrbird.febs.pedometer.service.IChickenService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 鸡 列表 Service实现
 *
 * @author li
 * @date 2020-02-01 19:38:49
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ChickenServiceImpl extends ServiceImpl<ChickenMapper, Chicken> implements IChickenService {

    @Autowired
    private ChickenMapper chickenMapper;

    @Override
    public IPage<Chicken> findChickens(QueryRequest request, Chicken chicken, Integer batchesId) {
        Page<Chicken> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, new QueryWrapper<Chicken>().lambda().in(Chicken::getBatchesId, batchesId));
    }

    @Override
    public List<Chicken> findChickens(Chicken chicken) {
	    LambdaQueryWrapper<Chicken> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createChicken(Chicken chicken) {
        this.save(chicken);
    }

    @Override
    @Transactional
    public void updateChicken(Chicken chicken) {
        chicken.setBatchesId(null);
        this.saveOrUpdate(chicken);
    }

    @Override
    @Transactional
    public void deleteChicken(Chicken chicken) {
        LambdaQueryWrapper<Chicken> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    public List<JSONObject> typeList() {

        return chickenMapper.typeList();
    }
}
